package com.zjsd.sqeybusiness.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.SecurityUtils;
import com.zjsd.sqeybusiness.domain.*;
import com.zjsd.sqeybusiness.mapper.*;
import com.zjsd.sqeybusiness.utils.WeChatAccessTokenUtil;
import com.zjsd.sqeybusiness.utils.WeChatTemplateMessageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zjsd.sqeybusiness.service.IOrderappealService;

/**
 * 订单申诉信息Service业务层处理
 * 
 * @author Zheng
 * @date 2025-03-11
 */
@Service
public class OrderappealServiceImpl implements IOrderappealService 
{
    @Autowired
    private OrderappealMapper orderappealMapper;
    @Autowired
    private OrderinfoMapper orderinfoMapper ;
    @Autowired
    private SjinfoMapper sjinfoMapper ;
    @Autowired
    private HyinfoMapper hyinfoMapper;
    @Autowired
    private AppealdetailMapper appealdetailMapper;
    @Autowired
    private WechatconfigMapper wechatconfigMapper;

    /**
     * 查询订单申诉信息
     * 
     * @param appealid 订单申诉信息ID
     * @return 订单申诉信息
     */
    @Override
    public Orderappeal selectOrderappealById(Long appealid)
    {
        Orderappeal orderappeal = orderappealMapper.selectOrderappealById(appealid);
        if(orderappeal != null){
            Appealdetail appelDetail = new Appealdetail();
            appelDetail.setAppealid(orderappeal.getAppealid());
            List<Appealdetail> list1 = appealdetailMapper.selectAppealdetailList(appelDetail);
            orderappeal.setList(list1);
        }
        return orderappeal;
    }

    /**
     * 查询订单申诉信息列表
     * 
     * @param orderappeal 订单申诉信息
     * @return 订单申诉信息
     */
    @Override
    public List<Orderappeal> selectOrderappealList(Orderappeal orderappeal)
    {
        List<Orderappeal> list = orderappealMapper.selectOrderappealList(orderappeal);
        if(list != null && list.size() > 0){
            for(Orderappeal orderappeal1 : list){
                Appealdetail appelDetail = new Appealdetail();
                appelDetail.setAppealid(orderappeal1.getAppealid());
                List<Appealdetail> list1 = appealdetailMapper.selectAppealdetailList(appelDetail);
                if(list1 != null && list1.size() > 0){
                    for(Appealdetail detail : list1){
                        orderappeal1.setZxhf(detail.getHfnr());
                        orderappeal1.setFksj(detail.getHfsj());
                    }
                }
                orderappeal1.setList(list1);
            }
        }
        return list;
    }

    /**
     * 新增订单申诉信息
     * 
     * @param orderappeal 订单申诉信息
     * @return 结果
     */
    @Override
    public int insertOrderappeal(Orderappeal orderappeal)
    {
        orderappeal.setSssj(new Date());
        if(orderappeal.getOrderid() != null){
            Orderinfo orderinfo = orderinfoMapper.selectOrderinfoById(orderappeal.getOrderid());
            if(orderinfo != null){
                orderappeal.setJdsj(orderinfo.getJdsj());
                orderinfo.setSszt("1");
                orderinfoMapper.updateOrderinfo(orderinfo);
            }
        }
        if(orderappeal.getSjid() != null){
            Sjinfo sjinfo = sjinfoMapper.selectSjinfoById(orderappeal.getSjid());
            if(sjinfo != null){
                orderappeal.setCgrphone(sjinfo.getCgrphone());
                orderappeal.setSjtitle(sjinfo.getTitle());
                orderappeal.setCgrname(sjinfo.getCgrname());
            }
        }
        System.out.println("进来了");
        if(orderappeal.getHyid() != null){
            Hyinfo hyinfo = hyinfoMapper.selectHyinfoById(orderappeal.getHyid());
            if(hyinfo != null){
                orderappeal.setWxname(hyinfo.getWxname());
                orderappeal.setHyname(hyinfo.getName());
                orderappeal.setHyphone(hyinfo.getPhone());
            }
        }
        orderappeal.setSszt("1");
        int i = orderappealMapper.insertOrderappeal(orderappeal);
        System.out.println("保存成功");
        this.sendMessage(orderappeal);
        return  i ;
    }

    private void sendMessage(Orderappeal orderappeal) {
        //TODO 待切换客服openid
        try {
            System.out.println("发送模板消息");
            String openId = "ouJl66ltZBY-a5ltxmNHMlOg7J0U";
            JSONObject data = new JSONObject();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String templateId = Constants.DDTZ_TEMPLATE_ID;
            String accessToken = "";
            Wechatconfig wechatconfig = wechatconfigMapper.selectWechatconfigById(1L);
            if(wechatconfig != null){
                accessToken =   WeChatAccessTokenUtil.getAccessToken(wechatconfigMapper);
            }
            //姓名
            data.put("thing1", new JSONObject().fluentPut("value", orderappeal.getHyphone()).fluentPut("color", "#173177"));
            //项目名称
            String title = "您有待处理的申诉信息";
            data.put("thing8", new JSONObject().fluentPut("value", title).fluentPut("color", "#173177"));
            //订单时间
            data.put("time9", new JSONObject().fluentPut("value", sdf.format(orderappeal.getSssj())).fluentPut("color", "#173177"));
            String result = WeChatTemplateMessageUtil.sendTemplateMessage(accessToken, openId, templateId, "", data);
            System.out.println("推送结果：" + result);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 修改订单申诉信息
     * 
     * @param orderappeal 订单申诉信息
     * @return 结果
     */
    @Override
    public int updateOrderappeal(Orderappeal orderappeal)
    {
        int i = 0  ;
        Orderappeal orderappeal1 = orderappealMapper.selectOrderappealById(orderappeal.getAppealid());
        if(orderappeal1 != null){
            orderappeal1.setSszt(orderappeal.getSszt());
            orderappeal1.setFksj(new Date());
            i = orderappealMapper.updateOrderappeal(orderappeal1);
            if(StringUtils.isNotBlank(orderappeal.getFknr())){
                Appealdetail appealdetail = new Appealdetail();
                appealdetail.setAppealid(orderappeal1.getAppealid());
                if(StringUtils.isNotBlank(orderappeal.getXthfbz()) && "1".equals(orderappeal.getXthfbz())){
                    LoginUser loginUser = SecurityUtils.getLoginUser();
                    appealdetail.setHfrid(loginUser.getUser().getUserId());
                    appealdetail.setHfrname(loginUser.getUser().getNickName());
                }else{
                    Hyinfo hyinfo = hyinfoMapper.selectHyinfoById(orderappeal1.getHyid());
                    if(hyinfo != null){
                        appealdetail.setHfrid(hyinfo.getHyid());
                        appealdetail.setHfrname(hyinfo.getName());
                    }
                }
                appealdetail.setHfnr(orderappeal.getFknr());
                appealdetail.setHfsj(new Date());
                appealdetailMapper.insertAppealdetail(appealdetail);
            }
        }
        return i ;
    }

    /**
     * 批量删除订单申诉信息
     * 
     * @param appealids 需要删除的订单申诉信息ID
     * @return 结果
     */
    @Override
    public int deleteOrderappealByIds(Long[] appealids)
    {
        return orderappealMapper.deleteOrderappealByIds(appealids);
    }

    /**
     * 删除订单申诉信息信息
     * 
     * @param appealid 订单申诉信息ID
     * @return 结果
     */
    @Override
    public int deleteOrderappealById(Long appealid)
    {
        return orderappealMapper.deleteOrderappealById(appealid);
    }
}
